extends Node2D

const GRID_CELL_SIZE = Vector2(100,100)

@onready var grid_cell:Texture2D =  preload("res://texture/game/grid.png")
@onready var red_cell:Texture2D =  preload("res://texture/game/block/yellow.png")

@export_group("网格大小","grid_")
## 网络行数
@export var grid_row: int = 2
## 网络列数
@export var grid_col: int = 5

var grid_offset: Vector2
var cell = Sprite2D.new()
var view_port: SubViewport

func _init() -> void:
	pass
	

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		#cell.position = cell.get_parent().to_local(event.position)
		pass
	
func _ready() -> void:
	await get_tree().root.ready
	view_port = self.get_viewport() as SubViewport
	draw_grid()

func draw_grid() -> void:
	var grid_size = GRID_CELL_SIZE * Vector2(grid_col, grid_row ) / 2
	grid_offset = (Vector2(view_port.size) - grid_size)/2
	if grid_offset < Vector2.ZERO:
		grid_offset = Vector2.ZERO
	
	grid_offset +=  GRID_CELL_SIZE
	
	# 批量生成
	for y in grid_row:
		for x in grid_col:
			var instance = Sprite2D.new()
			instance.position = grid_offset + GRID_CELL_SIZE*Vector2(x , y)
			instance.texture =  grid_cell
			instance.centered = true
			add_child(instance)
	
	
	#Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)  # 隐藏鼠标
	#cell.owner = root
	#cell.position = grid_offset + GRID_CELL_SIZE
	#cell.texture =  red_cell
	#cell.scale = (GRID_CELL_SIZE)/red_cell.get_size()
	#cell.centered = true
	#add_child(cell)

func _notification(what: int) -> void:
	pass
